//
//  StartViewController.m
//  States and Capitals
//
//  Created by Ward Fred on 12/3/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "StartViewController.h"
#import "States_and_CapitalsViewController.h"

@implementation StartViewController
@synthesize regionPicker;
@synthesize capitalButton, postalButton, goButton;
@synthesize northEast, southern, southWest, rockyMountain, midWest, pacific, myData;
@synthesize isCapitals, isPostal;
@synthesize sView;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
*/ 



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	isCapitals = YES;
	isPostal = YES;
	[super viewDidLoad];
	self.northEast = [NSArray arrayWithObjects:@"243" //MA
					  ,@"10" //CT
					  ,@"159" //NY
					  ,@"227" //NJ
					  ,@"155" //MD
					  ,@"158" //DI
					  ,@"226" //NH
					  ,@"11" //VT
					  ,@"157" //ME
					  ,@"9" //PA
					  ,@"244", nil]; //DE
	self.southern = [NSArray arrayWithObjects:@"161" //NC
					 ,@"248" //SC
					 ,@"247" //VA
					 ,@"228" //WV
					 ,@"7" //KY
					 ,@"229" //TN
					 ,@"252" //AK
					 ,@"8" //GA
					 ,@"249" //AL
					 ,@"163" //MS
					 ,@"231" //LA
					 ,@"162", nil]; //FL
	self.midWest = [NSArray arrayWithObjects:@"6" //MI
					,@"160" //IN
					,@"230" //WI
					,@"5" //IA
					,@"251" //251
					,@"3" //ND
					,@"165" //SD
					,@"246" //OH
					,@"253" //IL
					,@"254" //MN
					,@"164" //MO
					,@"233", nil]; //NE
	self.rockyMountain =[NSArray arrayWithObjects:@"235" //MT
						 ,@"245" //WY
						 ,@"1" //ID
						 ,@"166" //UT
						 ,@"250" //NV
						 ,@"2", nil]; //CO
	
	self.southWest = [NSArray arrayWithObjects:@"232" //OK
					  ,@"4" //TX
					  ,@"241" //NM
					  ,@"234", nil]; //AZ
	self.pacific = [NSArray arrayWithObjects:@"242" //WA
					,@"167" //OR
					,@"12" //CA
					,@"156" //AK
					,@"240", nil]; //HI
	//NSArray *index= [NSArray arrayWithObjects:[NSIndexPath indexPathWithIndex:0], nil];
	self.myData = [NSArray arrayWithObjects:@"North East", @"Southern", @"Midwest",@"Rocky Mountain",@"South West", @"Pacific", nil];
	regionPicker.delegate = self;
	regionPicker.dataSource = self;
	//[regionPicker insertRowsAtIndexPaths:index withRowAnimation:nil];
	[regionPicker reloadData];
	
}


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */
int randomSort(id obj1, id obj2, void *context)
{
	return(arc4random()%3-1);
}
-(IBAction)startQuizMode
{
	
	NSLog(@"current view control: %@", NSStringFromClass([self.navigationController.visibleViewController class]));
	sView = [[States_and_CapitalsViewController alloc] init];
	NSArray *regions = [NSArray arrayWithObjects:northEast, southern, midWest, rockyMountain, southWest, pacific, nil];
	NSMutableArray *forQuiz = [NSMutableArray array];
	for(NSIndexPath *index in [regionPicker indexPathsForVisibleRows]) //look through all rows
	{
		if([regionPicker cellForRowAtIndexPath:index].accessoryType == UITableViewCellAccessoryCheckmark) //if its checked
		{
			[forQuiz addObjectsFromArray:(NSArray *)[regions objectAtIndex:index.row]]; //add the number strings for the corresponding region (see regions array)
			NSLog(@"hello%@", forQuiz);
		}
	}
	if([forQuiz count] == 0) //if they chose no states
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Please choose a region or regions"] message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
		[alert show];
		[alert release];
		
	}
	else{
		[forQuiz sortUsingFunction:randomSort context:nil];
		sView.isPostal = isPostal; // set the variables in sView so it has its own set of the necessary data to hand down its view
		sView.isCapitals = isCapitals;
		sView.states = forQuiz;
		//[self.view addSubview:sView.view];
		//[self.navigationController setNavigationBarHidden:NO];
		[self.navigationController pushViewController:sView animated:NO];
		//[self.navigationController setNavigationBarHidden:YES animated:NO];
		//NSLog(@"current view control: %@", NSStringFromClass([navControl.visibleViewController class]));
	}
    [sView release];
}

-(IBAction) setPostal{//turn postal abbreviations on or off
	NSLog(@"%@",postalButton.titleLabel.text);
	//change the butoón 
	if([postalButton.titleLabel.text isEqualToString: @"Abbreviations On"]){
		[postalButton setTitle: @"Abbreviations Off" forState:UIControlStateNormal];
		//[postalButton setTitle: @"Capitals Off" forState:UIControlStateSelected];
		
		isPostal = NO;
		//NSLog(@"%@",capitalButton.titleLabel.text);
	}
	else if([postalButton.titleLabel.text isEqualToString:@"Abbreviations Off" ]){
		[postalButton setTitle: @"Abbreviations On" forState:UIControlStateNormal];
		//[postalbutton setTitle: @"Abbreviations On" forState:UIControlStateSelected];
		isPostal = YES;
	}
	
	
}

-(IBAction)setCapital{ //turn capital quizzing on or off
	NSLog(@"%@",capitalButton.titleLabel.text);
	
	if([capitalButton.titleLabel.text isEqualToString: @"Capitals On"]){
		[capitalButton setTitle: @"Capitals Off" forState:UIControlStateNormal];
		//[capitalButton setTitle: @"Capitals Off" forState:UIControlStateSelected];
		
		isCapitals = NO;
		//NSLog(@"%@",capitalButton.titleLabel.text);
	}
	else if([capitalButton.titleLabel.text isEqualToString:@"Capitals Off" ]){
		[capitalButton setTitle: @"Capitals On" forState:UIControlStateNormal];
		//[capitalButton setTitle: @"Capitals On" forState:UIControlStateSelected];
		isCapitals = YES;
	}
	
	
}

-(UIView *)getView
{
	return self.view;
}




//TableView Delegate Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [myData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *CellIdentifier = @"Cell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if(cell == nil)
	{
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
	}
	NSString *cellValue = [myData objectAtIndex:indexPath.row];
	
	cell.textLabel.text = cellValue;
	return cell;
	
}
-(UITableView *)tableView:tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	if(cell.accessoryType == UITableViewCellAccessoryNone)
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
	else
		cell.accessoryType = UITableViewCellAccessoryNone;
	return tableView;
}
//End TableView Delegate Methods

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSLog(@"StartView Touched");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
	/*BOOL rotate = NO;
	 if(sView)
	 rotate = [sView shouldAutorotateToInterfaceOrientation:interfaceOrientation];
	 if(!rotate){
	 [self 
	 }
	 */
	return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
            
    }
}



- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	postalButton =nil;
    capitalButton = nil;
    goButton = nil;
    regionPicker = nil;
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
    postalButton =nil;
    capitalButton = nil;
    goButton = nil;
    regionPicker = nil;
    
}


@end
